package com.jwb.pub.controller;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jwb.pub.dao.BaseDAO;
import com.jwb.pub.service.MsgService;
import com.jwb.util.MainController;
@Slf4j
@Controller
@RequestMapping(value="/pub/msg")
public class MsgController extends MainController{	

	@Autowired
	private BaseDAO baseDAO;  
	@Autowired
	private MsgService msgService;
    /**
     * 消息发布列表O
     *  http://127.0.0.1:8080/ssm/pub/msg/messageList
     * @param response
     * @throws Exception
     */
    @RequestMapping(value="/messageList")    
    @ResponseBody
    public String  messageList(HttpServletRequest req,HttpServletResponse response) throws Exception {
    
    	JSONObject jsobject = new JSONObject();   
    	HttpSession session = req.getSession();
    	String rankDeptId = (String) session.getAttribute("rankDeptId") == null ? "": (String) session.getAttribute("rankDeptId");
    	String currentPageNbr= req.getParameter("currentPageNbr") == null ? "1" : req.getParameter("currentPageNbr");
    	String pageSize= req.getParameter("pageSize") == null ? "10" : req.getParameter("pageSize");
    	String jsonStr = req.getParameter("jsonStr") == null ? "{}" : req.getParameter("jsonStr");
    	JSONObject jsdata = new JSONObject();
    	try {

			jsdata=JSONObject.parseObject(jsonStr);
			
			this.pageInit();
			this.getPage().setEveryPageCnt(Integer.parseInt(pageSize));
			this.getPage().setCurrentPageNbr(Integer.parseInt(currentPageNbr));
			jsdata.put("rankDeptId", rankDeptId);
			jsobject = msgService.messageList(jsdata, page);
    	
		} catch (Exception e) {
			e.printStackTrace();
			jsobject.put("respCode", "-3");
			jsobject.put("respDesc", "字符串格式有误");	

		}
    	log.info("messageList():" + jsobject.toString());
        return jsobject.toString();
    	
    	
    } 
    
    
    
    /**
     * 消息发布列表O
     *  http://127.0.0.1:8080/ssm/pub/msg/messageDel
     * @param response
     * @throws Exception
     */
    @RequestMapping(value="/messageDel")    
    @ResponseBody
    public String  messageDel(HttpServletRequest req,HttpServletResponse response) throws Exception {
    	HttpSession session = req.getSession();
    	String operCode = (String) session.getAttribute("userId") == null ? "": (String) session.getAttribute("userId");
    	JSONObject jsobject = new JSONObject();   
    	String jsonStr = req.getParameter("jsonStr") == null ? "{}" : req.getParameter("jsonStr");
    	JSONObject jsdata = new JSONObject();
    	try {

			jsdata=JSONObject.parseObject(jsonStr);
			jsdata.put("oper_code", operCode);			
			jsobject = msgService.messageDel(jsdata);
    	
		} catch (Exception e) {
			e.printStackTrace();
			jsobject.put("respCode", "-3");
			jsobject.put("respDesc", "字符串格式有误");	

		}
    	log.info("messageDel():" + jsobject.toString());
        return jsobject.toString();
    	
    	
    } 
    
    
    /**
     * 消息发布列表O
     *  http://127.0.0.1:8080/ssm/pub/msg/messageDel
     * @param response
     * @throws Exception
     */
    @RequestMapping(value="/messageSend")    
    @ResponseBody
    public String  messageSend(HttpServletRequest req,HttpServletResponse response) throws Exception {
    	HttpSession session = req.getSession();
    	String operCode = (String) session.getAttribute("userId") == null ? "": (String) session.getAttribute("userId");
    	JSONObject jsobject = new JSONObject();   
    	String jsonStr = req.getParameter("jsonStr") == null ? "{}" : req.getParameter("jsonStr");
    	JSONObject jsdata = new JSONObject();
    	try {

			jsdata=JSONObject.parseObject(jsonStr);
			jsdata.put("oper_code", operCode);			
			jsobject = msgService.messageSend(jsdata);
    	
		} catch (Exception e) {
			e.printStackTrace();
			jsobject.put("respCode", "-3");
			jsobject.put("respDesc", "字符串格式有误");	

		}
    	log.info("messageSend():" + jsobject.toString());
        return jsobject.toString();
    	
    	
    } 
    
    
    
    
    
    /**
     * 消息列表 接收明细表 
     * operType sendUser 发布者查看   recvUser 接收者本人
     * http://127.0.0.1:8080/services/msg/msgDetailList.mvc?msgId=1&startDate=2018-04-04
     * http://127.0.0.1:8080/services/msg/msgDetailList.mvc?operType=recvUser&readStatus=read_status_0
     * @param req
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/msgDetailList")    
    @ResponseBody
    public String  msgDetailList(HttpServletRequest req,HttpServletResponse response) throws Exception {
    
    	JSONObject jsobject = new JSONObject();   
    	HttpSession session = req.getSession();
    	String rankDeptId = (String) session.getAttribute("rankDeptId") == null ? "": (String) session.getAttribute("rankDeptId");
    	String currentPageNbr= req.getParameter("currentPageNbr") == null ? "1" : req.getParameter("currentPageNbr");
    	String pageSize= req.getParameter("pageSize") == null ? "10" : req.getParameter("pageSize");
    	String jsonStr = req.getParameter("jsonStr") == null ? "{}" : req.getParameter("jsonStr");
    	JSONObject jsdata = new JSONObject();
    	try {

			jsdata=JSONObject.parseObject(jsonStr);
			
			this.pageInit();
			this.getPage().setEveryPageCnt(Integer.parseInt(pageSize));
			this.getPage().setCurrentPageNbr(Integer.parseInt(currentPageNbr));
			
			jsobject = msgService.msgDetailList(jsdata, page);
    	
		} catch (Exception e) {
			e.printStackTrace();
			jsobject.put("respCode", "-3");
			jsobject.put("respDesc", "字符串格式有误");	

		}
    	log.info("msgDetailList():" + jsobject.toString());
        return jsobject.toString();
    	
    	
    } 
    
    /**
     * 消息信息
     *  http://127.0.0.1:8080/ssm/pub/msg/messageInfo?jsonStr={"msg_id":"1"}
     * @param response
     * @throws Exception
     */
    @RequestMapping(value="/messageInfo")    
    @ResponseBody
    public String  messageInfo(HttpServletRequest req,HttpServletResponse response) throws Exception {
    
    	JSONObject jsobject = new JSONObject();   
    	HttpSession session = req.getSession();
    	String rankDeptId = (String) session.getAttribute("rankDeptId") == null ? "": (String) session.getAttribute("rankDeptId");
    	String jsonStr = req.getParameter("jsonStr") == null ? "{}" : req.getParameter("jsonStr");
    	JSONObject jsdata = new JSONObject();
    	try {

			jsdata=JSONObject.parseObject(jsonStr);	
			
		
			jsobject = msgService.messageInfo(jsdata);
    	
		} catch (Exception e) {
			e.printStackTrace();
			jsobject.put("respCode", "-3");
			jsobject.put("respDesc", "字符串格式有误");	

		}
    	log.info("messageInfo():" + jsobject.toString());
        return jsobject.toString();
    	
    	
    } 
    
    
    
    /**
     * operType edit, add 
     * msgId
     * http://127.0.0.1:8080/ssm/pub/msg/messageSave.mvc
     * 消息保存 添加，编辑,发布
     * @param req
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/messageSave")    
    @ResponseBody
    public String  messageSave(HttpServletRequest req,HttpServletResponse response) throws Exception {
    	JSONObject jsobject = new JSONObject();  
    	HttpSession session = req.getSession();
 	    String userId=(String)session.getAttribute("userId")  == null ? "" : (String)session.getAttribute("userId");    	       
    	String jsonStr = req.getParameter("jsonStr") == null ? "{}" : req.getParameter("jsonStr");
    	JSONObject jsdata = new JSONObject();
    	try {

			jsdata=JSONObject.parseObject(jsonStr);
			jsdata.put("oper_code", userId);			
			jsobject = msgService.messageSave(jsdata);
			//int newId=baseDAO.getMaxId("msg", "msg_id");
		//	System.out.println("newId="+newId);
    	
		} catch (Exception e) {
			e.printStackTrace();
			jsobject.put("respCode", "-3");
			jsobject.put("respDesc", "字符串格式有误");	

		}
    	log.info("messageSave():" + jsobject.toString());
    	return jsobject.toString();	
    }
    
    /**
     * http://127.0.0.1:8080/ssm/pub/msg/getMaxId.mvc
     * @param req
     * @param response
     * @return
     * @throws Exception
     */
    
    @RequestMapping(value="/getMaxId")    
    @ResponseBody
    public String  getMaxId(HttpServletRequest req,HttpServletResponse response) throws Exception {
    	JSONObject jsobject = new JSONObject();      
    	String jsonStr = req.getParameter("jsonStr") == null ? "{}" : req.getParameter("jsonStr");
    	JSONObject jsdata = new JSONObject();
    	try {
    		jsdata=JSONObject.parseObject(jsonStr);
	    	int newId=baseDAO.getMaxId("msg", "msg_id");
	    	if(newId>0)
	    	{
	    		jsobject.put("msg_id", newId);
				jsobject.put("respCode", "0");
				jsobject.put("respDesc", "");	
	    	}
	    	else
	    	{
	    	
				jsobject.put("respCode", "-1");
				jsobject.put("respDesc", "");	
	    	}
    	}
    	 catch (Exception e) {
 			e.printStackTrace();
 			jsobject.put("respCode", "-3");
 			jsobject.put("respDesc", "字符串格式有误");	

 		}

    	log.info("getMaxId():" + jsobject.toString());
    	return jsobject.toString();	
    }
    
    
    
    
    /**
     * 消息发布列表O
     *  http://127.0.0.1:8080/ssm/pub/msg/msgDetailDel
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value="/msgDetailDel")    
    @ResponseBody
    public String  msgDetailDel(HttpServletRequest req,HttpServletResponse response) throws Exception {
    	HttpSession session = req.getSession();
    	String operCode = (String) session.getAttribute("userId") == null ? "": (String) session.getAttribute("userId");
    	JSONObject jsobject = new JSONObject();   
    	String jsonStr = req.getParameter("jsonStr") == null ? "{}" : req.getParameter("jsonStr");
    	JSONObject jsdata = new JSONObject();
    	try {

			jsdata=JSONObject.parseObject(jsonStr);
			jsdata.put("oper_code", operCode);			
			jsobject = msgService.msgDetailDel(jsdata);
    	
		} catch (Exception e) {
			e.printStackTrace();
			jsobject.put("respCode", "-3");
			jsobject.put("respDesc", "字符串格式有误");	

		}
    	log.info("msgDetailDel():" + jsobject.toString());
        return jsobject.toString();
    	
    	
    } 
    
    
    /**
     * 用户消息接收列表
     * http://127.0.0.1:8080/ssm/pub/msg/revMessageList?jsonStr={}
     * startDate,endDate,msgType,msg_title,read_status
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value="/revMessageList")    
    @ResponseBody
    public String  revMessageList(HttpServletRequest req,HttpServletResponse response) throws Exception {
    
    	JSONObject jsobject = new JSONObject();   
    	HttpSession session = req.getSession();
 	    String userId=(String)session.getAttribute("userId")  == null ? "" : (String)session.getAttribute("userId");  
 	    String currentPageNbr= req.getParameter("currentPageNbr") == null ? "1" : req.getParameter("currentPageNbr");
    	String pageSize= req.getParameter("pageSize") == null ? "10" : req.getParameter("pageSize");
    	String jsonStr = req.getParameter("jsonStr") == null ? "{}" : req.getParameter("jsonStr");
    	JSONObject jsdata = new JSONObject();
    	try {

			jsdata=JSONObject.parseObject(jsonStr);
			
			this.pageInit();
			this.getPage().setEveryPageCnt(Integer.parseInt(pageSize));
			this.getPage().setCurrentPageNbr(Integer.parseInt(currentPageNbr));
			jsdata.put("recv_oper_code", userId);
			jsobject = msgService.revMessageList(jsdata, page);
    	
		} catch (Exception e) {
			e.printStackTrace();
			jsobject.put("respCode", "-3");
			jsobject.put("respDesc", "字符串格式有误");	

		}
    	log.info("revMessageList():" + jsobject.toString());
        return jsobject.toString();
    	
    	
    } 
    
    
    /**
     * 用户消息读取
     * http://127.0.0.1:8080/ssm/pub/msg/userReadMsg?jsonStr={}
     * detail_id
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value="/userReadMsg")    
    @ResponseBody
    public String  userReadMsg(HttpServletRequest req,HttpServletResponse response) throws Exception {
    
    	JSONObject jsobject = new JSONObject();   
    	HttpSession session = req.getSession();
 	    String userId=(String)session.getAttribute("userId")  == null ? "" : (String)session.getAttribute("userId");  
    	String jsonStr = req.getParameter("jsonStr") == null ? "{}" : req.getParameter("jsonStr");
    	JSONObject jsdata = new JSONObject();
    	try {

			jsdata=JSONObject.parseObject(jsonStr);
			jsdata.put("recv_oper_code", userId);
			jsobject = msgService.userReadMsg(jsdata);
    	
		} catch (Exception e) {
			e.printStackTrace();
			jsobject.put("respCode", "-3");
			jsobject.put("respDesc", "字符串格式有误");	

		}
    	log.info("userReadMsg():" + jsobject.toString());
        return jsobject.toString();
    	
    	
    } 
    
    
    
    /**
     * 用户评论消息
     * http://127.0.0.1:8080/ssm/pub/msg/userCommMsg?jsonStr={}
     * detail_id
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value="/userCommMsg")    
    @ResponseBody
    public String  userCommMsg(HttpServletRequest req,HttpServletResponse response) throws Exception {
    
    	JSONObject jsobject = new JSONObject();      	
    	String jsonStr = req.getParameter("jsonStr") == null ? "{}" : req.getParameter("jsonStr");
    	JSONObject jsdata = new JSONObject();
    	try {

			jsdata=JSONObject.parseObject(jsonStr);
			jsobject = msgService.userCommMsg(jsdata);
    	
		} catch (Exception e) {
			e.printStackTrace();
			jsobject.put("respCode", "-3");
			jsobject.put("respDesc", "字符串格式有误");	

		}
    	log.info("userCommMsg():" + jsobject.toString());
        return jsobject.toString();
    	
    	
    } 
    
    
    /**
     * 消息列表 接收明细表     * 
     * http://127.0.0.1:8080/ssm/pub/msg/msgDetailInfo.mvc?jsonStr={"detail_id":"923"}
     * detail_id
     * @param req
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/msgDetailInfo")    
    @ResponseBody
    public String  msgDetailInfo(HttpServletRequest req,HttpServletResponse response) throws Exception {
    	JSONObject jsobject = new JSONObject();      	
    	String jsonStr = req.getParameter("jsonStr") == null ? "{}" : req.getParameter("jsonStr");
    	JSONObject jsdata = new JSONObject();
    	try {

			jsdata=JSONObject.parseObject(jsonStr);
			jsobject = msgService.msgDetailInfo(jsdata);
    	
		} catch (Exception e) {
			e.printStackTrace();
			jsobject.put("respCode", "-3");
			jsobject.put("respDesc", "字符串格式有误");	

		}
    	log.info("msgDetailInfo():" + jsobject.toString());
        return jsobject.toString();
    	
    }
    
    
    
    /**
     * 用户消息接收列表
     * http://127.0.0.1:8080/ssm/pub/msg/msgList?jsonStr={}
     * read_status  阅读状态 read_status_0:未阅读 read_status_1:已阅读
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value="/msgList")    
    @ResponseBody
    public String  msgList(HttpServletRequest req,HttpServletResponse response) throws Exception {
    
    	JSONObject jsobject = new JSONObject();   
    	HttpSession session = req.getSession();
 	    String userId=(String)session.getAttribute("userId")  == null ? "" : (String)session.getAttribute("userId");  
 	    String currentPageNbr= req.getParameter("currentPageNbr") == null ? "1" : req.getParameter("currentPageNbr");
   	    String pageSize= req.getParameter("pageSize") == null ? "5" : req.getParameter("pageSize");
 	    String jsonStr = req.getParameter("jsonStr") == null ? "{}" : req.getParameter("jsonStr");
    	JSONObject jsdata = new JSONObject();
    	try {

			jsdata=JSONObject.parseObject(jsonStr);		
			
			this.pageInit();
			this.getPage().setEveryPageCnt(Integer.parseInt(pageSize));
			this.getPage().setCurrentPageNbr(Integer.parseInt(currentPageNbr));
			jsdata.put("recv_oper_code", userId);
			jsobject = msgService.msgList(jsdata,page);
    	
		} catch (Exception e) {
			e.printStackTrace();
			jsobject.put("respCode", "-3");
			jsobject.put("respDesc", "字符串格式有误");	

		}
    	log.info("msgList():" + jsobject.toString());
        return jsobject.toString();
    	
    	
    } 
}